package utils

import (
	"bytes"
	"fmt"
	"github.com/sirupsen/logrus"
	"strings"
)

type Formatter struct{}

func (f *Formatter) Format(entry *logrus.Entry) ([]byte, error) {
	var out string
	if entry.HasCaller() {
		var pkg bytes.Buffer
		for _, t := range strings.Split(entry.Caller.File, "/") {
			if len(t) == 0 {
				continue;
			}
			pkg.WriteString(t[:1])
			pkg.WriteString(".")
		}
		pkg.WriteString(entry.Caller.Function)

		out = fmt.Sprintf("[%s][%s] %s(%d) %s\n", entry.Level.String()[:4], entry.Time.Format("2006-01-02 15:03:04.000"), pkg.String(), entry.Caller.Line, entry.Message)
	} else {
		out = fmt.Sprintf("[%s][%s] %s\n", entry.Level.String()[:4], entry.Time.Format("2006-01-02 15:03:04.000"), entry.Message)
	}
	return []byte(out), nil
}